package yingyong;

import Studentlei.Att;
import Studentlei.Student;
import Studentlei.xuesheng;
import gongju.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(name = "add", value = "/add")
public class add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setContentType("text/html;charset=utf8");
//        String sql = "SELECT aid,s.sid,s.sname,time,type FROM attence a , student s WHERE a.sid=s.sid";
        String sql = "select *from Student";
        ArrayList<Student> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try {
            conn = DBUtil.getcoon();
            pre = conn.prepareStatement(sql);
            re = pre.executeQuery();
            while (re.next()) {
                int sid = re.getInt("sid");
                String sname = re.getString("sname");
                Student st = new Student(sid, sname);
                list.add(st);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,re,pre);
        }
        request.setAttribute("list",list);
        request.getRequestDispatcher("WEB-INF/tianjia.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
